package com.ybt.heartratem.common.app;

import android.os.Environment;

import com.ybt.heartratem.common.utils.FileUtil;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;

/**
 * 项目缓存管理类
 *
 * @author tony.liu
 */
public class CacheManager {
    private static final String TAG = CacheManager.class.getCanonicalName();
    //*************有SD卡时路径******start******//
    // SD卡路径
    public static final String SD_DIR = Environment.getExternalStorageDirectory().getPath();
    // 项目路径
    public static final String SD_APP_DIR = SD_DIR + "/DZC-YBT";
    // 异常日志路径
    public static final String SD_CRASH_DIR = SD_APP_DIR + "/crash";
    // 图片路径
    public static final String SD_IMAGE_DIR = SD_APP_DIR + "/image";
    // 下载APP路径
    public static final String SD_APK_DIR = SD_APP_DIR + "/apk";
    public static final String SD_SAVE_VOICE_DIR = SD_APP_DIR + "/voice";
    //*************有SD卡时路径******end******//

    public CacheManager() {
        init();
    }

    public void init() {
        // 删除以前的残留文件夹
        FileUtil.deleteDirectory(SD_DIR + "/DZC-YBT_");
        createProjectDir();
    }

    private void createProjectDir() {
        if (FileUtil.hasSDCard()) {
            File rootDir = new File(SD_APP_DIR);
            File crashDir = new File(SD_CRASH_DIR);
            File imageDir = new File(SD_IMAGE_DIR);
            File apkDir = new File(SD_APK_DIR);
            File voiceDir = new File(SD_SAVE_VOICE_DIR);

            FileUtil.mkdirs(rootDir);
            FileUtil.mkdirs(crashDir);
            FileUtil.mkdirs(imageDir);
            FileUtil.mkdirs(apkDir);
            FileUtil.mkdirs(voiceDir);
        }
    }

    /***
     * Json数据缓存到本地
     *
     * @param json
     */
    public static void wirteLocalTargetFile(String json, String filePath) {
        try {
            File file = new File(filePath);
            if (file.exists()) {
                file.delete();
            }
            OutputStreamWriter write;
            BufferedWriter out;
            write = new OutputStreamWriter(new FileOutputStream(filePath), Charset.forName("gbk"));

            out = new BufferedWriter(write, 1024);
            out.write(json);
            out.flush();
            out.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /***
     * 读取本地缓存的Json
     *
     * @return
     */
    public static String readLocalTargetFile(String filePath) {
        String res = "";
        try {
            File file = new File(filePath);
            if (file.exists()) {
                FileInputStream fin = new FileInputStream(filePath);
                int length = fin.available();
                byte[] buffer = new byte[length];
                fin.read(buffer);
                res = new String(buffer, "gbk");
                fin.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return res;
    }

    public static String readLocalFile(String filePath) {
        String res = "";
        try {
            File file = new File(filePath);
            if (file.exists()) {
                FileInputStream fin = new FileInputStream(filePath);
                int length = fin.available();
                byte[] buffer = new byte[length];
                fin.read(buffer);
                res = new String(buffer, "UTF-8");
                fin.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return res;
    }

    /***
     * 删除指定的json文件
     *
     * @param filePath
     */
    public static void delJsonFile(String filePath) {
        try {
            File file = new File(filePath);
            if (file.exists()) {
                file.delete();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
